import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/constant/constant.dart';
import 'package:nyh_motorcade_app/entity/card_details_entity.dart';
import 'package:nyh_motorcade_app/entity/energy_type_entity.dart';
import 'package:nyh_motorcade_app/http/http_address.dart';
import 'package:nyh_motorcade_app/http/http_manager.dart';
import 'package:nyh_motorcade_app/routes/routes.dart';
import 'package:nyh_motorcade_app/utils/sp_utils.dart';
import 'package:nyh_motorcade_app/utils/toast_utils.dart';

class AddCardController extends GetxController {
  TextEditingController cardNameController = TextEditingController();
  TextEditingController remarksController = TextEditingController();
  FocusNode cardNameFocus = FocusNode();
  FocusNode remarksFocus = FocusNode();

  //是不是自有卡(还有外协卡)
  bool isUserCard = true;
  String cardName = "";
  String energyType = "";
  String energyTypeName = "";
  bool state = true;
  String remarks = "";
  List<EnergyTypeEntity> energyTypeList = [];

  //编辑时参数
  CardDetailsEntity? cardDetailsEntity;

  //是不是开卡(还有编辑卡)
  bool isAddCard = true;

  //是否返回
  bool isReturn = false;

  @override
  void onReady() {
    super.onReady();
    initData();
  }

  void changeCardType() {
    isUserCard = !isUserCard;
    update();
  }

  void changeState(bool value) {
    state = value;
    update();
  }

  void setCardName(String value) {
    cardName = value;
    update();
  }

  void setRemarks(String value) {
    remarks = value;
  }

  bool isComplete() {
    if (cardName.isEmpty) {
      return false;
    }
    if (energyType.isEmpty) {
      return false;
    }
    return true;
  }

  void onConfirm() {
    if (!isComplete()) {
      return;
    }
    clearFocus();
    if (isAddCard) {
      activateCard();
    } else {
      editCard();
    }
  }

  Future<void> activateCard() async {
    httpManager.post(
      Get.context,
      HttpAddress.ACTIVATE_CARD,
      {
        "superId": await SPUtils.getString(SPUtils.COMPANY_ID),
        "cardType": isUserCard ? Constant.OWN_CARD : Constant.EXTERNAL_CARD,
        "name": cardName,
        "energyType": energyType,
        "status": state ? 1 : 0,
        "remark": remarks,
      },
      isToast: true,
      isLoadingDialog: true,
      onSuccess: (data) {
        if (data == null) {
          ToastUtils.toast("请求失败");
          return;
        }
        if (isReturn) {
          Get.back(result: {});
        } else {
          Get.offNamed(Routes.cardDetails, arguments: data.toString());
        }
      },
    );
  }

  Future<void> editCard() async {
    if (cardDetailsEntity == null) {
      return;
    }
    httpManager.post(
      Get.context,
      HttpAddress.EDIT_CARD,
      {
        "superId": await SPUtils.getString(SPUtils.COMPANY_ID),
        "id": cardDetailsEntity!.id ?? "",
        "cardType": isUserCard ? Constant.OWN_CARD : Constant.EXTERNAL_CARD,
        "name": cardName,
        "energyType": energyType,
        "status": state ? 1 : 0,
        "remark": remarks,
      },
      isToast: true,
      isLoadingDialog: true,
      onSuccess: (data) {
        if (data == null) {
          ToastUtils.toast("请求失败");
          return;
        }
        Get.back(result: {});
      },
    );
  }

  void changeEnergyType(int index) {
    String type = energyTypeList[index].dictdataValue ?? "";
    String name = energyTypeList[index].dictdataName ?? "";
    if (energyType != type) {
      energyType = type;
      energyTypeName = name;
      update();
    }
  }

  Future<List<EnergyTypeEntity>> getEnergyType() async {
    if (energyTypeList.isNotEmpty) {
      return energyTypeList;
    }
    HttpResult httpResult = await httpManager.post(Get.context, HttpAddress.GET_ENERGY_TYPE, {}, isToast: true, isLoadingDialog: true);
    dynamic data = httpResult.data;
    if (data == null) {
      return [];
    }
    if (data is List) {
      energyTypeList = data.map((element) {
        return EnergyTypeEntity().fromJson((element as Map<String, dynamic>));
      }).toList();
      return energyTypeList;
    }
    return [];
  }

  void clearFocus() {
    cardNameFocus.unfocus();
    remarksFocus.unfocus();
  }

  void initData() {
    dynamic arguments = Get.arguments;
    if (arguments != null && arguments is Map) {
      dynamic data = arguments[Constant.PARAM_ENTITY];
      isReturn = arguments[Constant.PARAM_IS_RETURN] ?? false;
      if (data != null) {
        isAddCard = false;
        cardDetailsEntity = data as CardDetailsEntity;
        String cardType = cardDetailsEntity!.cardType ?? "";
        isUserCard = cardType.isEmpty || cardType == Constant.OWN_CARD;
        cardName = cardDetailsEntity!.name ?? "";
        cardNameController.text = cardName;
        energyType = cardDetailsEntity!.energyType ?? "";
        energyTypeName = cardDetailsEntity!.energyTypeName ?? "";
        state = (cardDetailsEntity!.status ?? 0) == 1;
        remarks = cardDetailsEntity!.remark ?? "";
        remarksController.text = remarks;
        update();
      }
    }
  }
}
